package com.friends.library.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.friends.library.Dto.BookRegistration;
import com.friends.library.persistence.HibernateUtil;

public class SearchBookId extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     * @author HAVE A NICE DAY
     */
    public SearchBookId() {
        super();
        // TODO Auto-generated constructor stub
    }

    private ServletConfig config;

    //Setting JSP page

    String page="displayid.jsp";

    public void init(ServletConfig config)

    throws ServletException{

   this.config=config;
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// TODO Auto-generated method stub
		int bookid=Integer.parseInt(request.getParameter("bookid"));
PrintWriter out=response.getWriter();
response.setContentType("text/html");
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction transaction=session.beginTransaction();
String sql="from BookRegistration as book where book.bookid=:bookid";
Query query=session.createQuery(sql);
query.setInteger("bookid", bookid);
List datalist=new ArrayList();
for(Iterator iterator=query.iterate();iterator.hasNext();){
	BookRegistration bookRegistration=(BookRegistration)iterator.next();
	datalist.add(bookRegistration.getBookid());
	datalist.add(bookRegistration.getBookname());
	datalist.add(bookRegistration.getAuthorname());
	datalist.add(bookRegistration.getDate());
	datalist.add(bookRegistration.getStatus());
	
}
request.setAttribute("data",datalist);

RequestDispatcher dispatcher = request.getRequestDispatcher(page);

if (dispatcher != null){

dispatcher.forward(request, response);

} 

transaction.commit();
session.close();
	}

}
